﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace linkouqs.Gamekit
{

    public class Knaspack :Inventory
    {
        private static Knaspack _instance;
        public static Knaspack Instance
        {
            get
            {
                if(_instance==null)
                {
                    _instance = GameObject.Find("KnapspackPanel(Clone)").GetComponent<Knaspack>();
                    
                }
                return _instance;
            }
        }

        

        private Button useItemBtn;
        private Button dropItemBtn;
        private Button unEquipItemBtn;

      
       
        private EquipmentSlot[] equipSlots;

        public EquipmentSlot GetEquipmentSlotByID(int id)
        {
            foreach(EquipmentSlot tempSlot in equipSlots)
            {
                if(tempSlot.ID==id)
                {
                    return tempSlot;
                }
            }
            return null;
        }

     

        public override void Start()
        {
       
            base.Start();
            useItemBtn = transform.Find("UseItemBtn").gameObject.GetComponent<Button>();
            dropItemBtn = transform.Find("DropItemBtn").gameObject.GetComponent<Button>();
            unEquipItemBtn = transform.Find("UnEquipItemBtn").gameObject.GetComponent<Button>();

            useItemBtn.onClick.AddListener(UseItemClick);
            dropItemBtn.onClick.AddListener(DropItemClick);
            unEquipItemBtn.onClick.AddListener(UnEquipItemClick);



            equipSlots=transform.Find("EquipmentSlots").GetComponentsInChildren<EquipmentSlot>();

           
        }

       

        public void PutOn(Item item,Slot slot)
        {
            Item exitItem = null;
          
                 
                    if(slot.transform.childCount>0)
                    {
                        ItemUI currentItemUI = slot.transform.GetChild(0).GetComponent<ItemUI>();
                        exitItem = currentItemUI.Item;
                        currentItemUI.SetItem(item, 1);
                    }
                    else
                    {
                        slot.StoreItem(item);
                    }
                   
           
            

            if(exitItem!=null)
            {
                Knaspack.Instance.StoreItem(exitItem);
            }
        }

        public void PutOff(Item item)
        {
           Knaspack.Instance.StoreItem(item);
        }

        public void UseItemClick()
        {
            if (InventoryManager.Instance.PickedItem == null) return;           
            ShowSelectCharacter();
        }

        public void DropItemClick()
        {
            if (InventoryManager.Instance.PickedItem != null)
            {
                InventoryManager.Instance.RemoveItem(InventoryManager.Instance.PickedItem.Amount);                        
            }
            else
            {
                Debug.LogWarning("该物品槽没有物品无法丢弃");
            }
        }

        public void  UnEquipItemClick()
        {
            Transform temp = InventoryManager.Instance.selectSlot.transform.GetChild(0);
            if (temp== null) return;
            ItemUI checkItem =temp.GetComponent<ItemUI>();
           
            if(temp.GetComponent<ItemUI>().Item.ItemType==ItemType.Weapon|| temp.GetComponent<ItemUI>().Item.ItemType == ItemType.UnConsumableProp)
            {
                PutOff(checkItem.Item);
                DestroyImmediate(temp.gameObject);
            }
            else
            {
                Debug.LogWarning("请选择你要卸下的装备(道具)槽");
            }
        }

        public void ShowSelectCharacter()
        {
            UIManager.Instance.PushPanel(UIPanelType.SelectCharacterPanel);
        }

        
    }
}
